package com.common.web;

//import javax.inject.Inject;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.manager.MenuInfoManager;
import com.common.manager.StaffManager;
import com.common.model.MenuInfoModel;
import com.common.model.StaffInfoModel;
import com.common.utils.SysConfig;
import com.framework.web.BaseController;

@Controller
public class IndexController extends BaseController{

	
    @Autowired
	private MenuInfoManager menuInfoManager;
    
	 
    @Autowired
	private StaffManager staffManager;
    
    /**
     * 功能：登出系统
     * @param request
     * @param response
     * @throws IOException
     */
	@RequestMapping("logout")
	public void logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.getSession().removeAttribute(SysConfig.ATTRIBUTE_USER_KEY);
		response.sendRedirect(request.getContextPath() +  "/login.jsp");
	}
	
	/**
	 * 功能：登录系统
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response) {
		String loginName = request.getParameter("username");
		String password = request.getParameter("passwordhash");
		try {
			StaffInfoModel userInfoModel = staffManager.checkLoginUser(loginName,password);
			if(userInfoModel!=null){
				request.getSession().setAttribute(SysConfig.ATTRIBUTE_USER_KEY,userInfoModel);
				List<MenuInfoModel> menuist = menuInfoManager.getTopMenuByRoleId(userInfoModel.getRoleids());
				request.setAttribute("menuList",menuist);
			}else{
				response.sendRedirect(request.getContextPath() +  "/login.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "/management/index";
	}

	/**
	 * 功能：系统主页
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		return "/management/index";
	}

	

	/**
	 * 功能：系统主界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("indexLayout")
	public String indexLayout(HttpServletRequest request, HttpServletResponse response) {
		
		return "/management/index_layout";
	}
}